/*
 * Created by JFormDesigner on Thu Apr 14 09:45:02 CST 2022
 */

package com.ZengQi.event.view;

import java.awt.event.*;
import com.ZengQi.event.controller.VisitorSearchViewController;
import com.ZengQi.event.entity.Topic;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/**
 * @author unknown
 */
public class VisitorSearchView extends JFrame {
    private String topicName;

    private String topicPublisher;

    private String topicContent;

    private  VisitorsMainView visitorsMainView;

    List<Topic> list=null;


    public VisitorSearchView(VisitorsMainView visitorsMainView) {
        this.visitorsMainView=visitorsMainView;
        VisitorSearchViewController visitorSearchViewController=new VisitorSearchViewController(visitorsMainView,this);

        initComponents();
       //题目初始化
        Topic conditionTopic=new Topic();
        //模糊查询
        conditionTopic.setTopicName("%"+visitorsMainView.getSearchTxt().getText()+"%");
        list = visitorSearchViewController.topicList(conditionTopic);
        if(list!=null){
            int i=0;
            DefaultTableModel dtm = (DefaultTableModel) topicTable.getModel();
            dtm.setRowCount(0);
            for(Topic topic:list){
                i=i+1;
                Vector<String> vector=new Vector<String>();
                vector.add(topic.getTopicName());
                vector.add(topic.getTopicPublisher());
                vector.add(topic.getTopicContent());
                dtm.addRow(vector);
            }
        }else{
            JOptionPane.showMessageDialog(visitorsMainView,"未找到相关题目！");
            new VisitorsMainView();
            return;



        }
        setVisible(true);

        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

    private void topicTableMousePressed(MouseEvent e) {
        tableMousePressed(e);
    }

    private void button1(ActionEvent e) {

        backBtnActionPerformed(e);
    }

    private void backBtnActionPerformed(ActionEvent e) {
        new VisitorsMainView();
        dispose();

    }
//查看评论按钮的事件监听
    private void lookCommentBtn(ActionEvent e) {
        lookCommentBtnActionPerformed(e);
    }

    private void lookCommentBtnActionPerformed(ActionEvent e) {
        new LookCommentView(topicName);
    }

    /**
     * 组件初始化
     */
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        scrollPane1 = new JScrollPane();
        topicTable = new JTable();
        button1 = new JButton();
        button2 = new JButton();
        panel1 = new JPanel();
        label3 = new JLabel();
        scrollPane2 = new JScrollPane();
        topicContentArea = new JTextArea();
        topicNameLabel = new JLabel();
        topicNameTxt = new JTextField();
        topicPublisherLabel = new JLabel();
        topicPublisherTxt = new JTextField();

        //======== this ========
        setTitle("\u505a\u9898\u5e2e");
        setIconImage(new ImageIcon(getClass().getResource("/bang.png")).getImage());
        setResizable(false);
        Container contentPane = getContentPane();

        //======== scrollPane1 ========
        {

            //---- topicTable ----
            topicTable.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                },
                new String[] {
                    "\u9898\u76ee\u540d\u79f0", "\u51fa\u9898\u4eba", "\u9898\u76ee\u5185\u5bb9"
                }
            ) {
                boolean[] columnEditable = new boolean[] {
                    false, false, false
                };
                @Override
                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return columnEditable[columnIndex];
                }
            });
            {
                TableColumnModel cm = topicTable.getColumnModel();
                cm.getColumn(0).setResizable(false);
                cm.getColumn(0).setMinWidth(50);
                cm.getColumn(0).setMaxWidth(240);
                cm.getColumn(0).setPreferredWidth(240);
                cm.getColumn(1).setResizable(false);
                cm.getColumn(1).setMaxWidth(140);
                cm.getColumn(1).setPreferredWidth(140);
                cm.getColumn(2).setResizable(false);
                cm.getColumn(2).setMaxWidth(300);
                cm.getColumn(2).setPreferredWidth(300);
            }
            topicTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            topicTable.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    topicTableMousePressed(e);
                }
            });
            scrollPane1.setViewportView(topicTable);
        }

        //---- button1 ----
        button1.setText("\u8fd4\u56de");
        button1.addActionListener(e -> button1(e));

        //---- button2 ----
        button2.setText("\u67e5\u770b\u8bc4\u8bba");
        button2.addActionListener(e -> lookCommentBtn(e));

        //======== panel1 ========
        {

            //---- label3 ----
            label3.setText("\u5185\u5bb9");

            //======== scrollPane2 ========
            {

                //---- topicContentArea ----
                topicContentArea.setEditable(false);
                scrollPane2.setViewportView(topicContentArea);
            }

            GroupLayout panel1Layout = new GroupLayout(panel1);
            panel1.setLayout(panel1Layout);
            panel1Layout.setHorizontalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(label3, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(scrollPane2, GroupLayout.PREFERRED_SIZE, 617, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(21, Short.MAX_VALUE))
            );
            panel1Layout.setVerticalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addComponent(label3)
                                .addContainerGap(146, Short.MAX_VALUE))
                            .addComponent(scrollPane2, GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)))
            );
        }

        //---- topicNameLabel ----
        topicNameLabel.setText("\u9898\u76ee\u540d\u79f0");

        //---- topicNameTxt ----
        topicNameTxt.setEditable(false);

        //---- topicPublisherLabel ----
        topicPublisherLabel.setText("\u51fa\u9898\u4eba");

        //---- topicPublisherTxt ----
        topicPublisherTxt.setEditable(false);

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(194, 194, 194)
                    .addComponent(button1)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 187, Short.MAX_VALUE)
                    .addComponent(button2)
                    .addGap(168, 168, 168))
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addContainerGap(35, Short.MAX_VALUE)
                    .addComponent(panel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(77, 77, 77)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(topicNameLabel, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(topicNameTxt, GroupLayout.PREFERRED_SIZE, 350, GroupLayout.PREFERRED_SIZE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(topicPublisherLabel, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(topicPublisherTxt, GroupLayout.PREFERRED_SIZE, 359, GroupLayout.PREFERRED_SIZE))
                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 582, GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(84, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(topicNameLabel, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
                            .addGap(6, 6, 6)
                            .addComponent(topicPublisherLabel)
                            .addGap(8, 8, 8))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(topicNameTxt, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(topicPublisherTxt, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)))
                    .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(button1)
                        .addComponent(button2))
                    .addContainerGap())
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JScrollPane scrollPane1;
    private JTable topicTable;
    private JButton button1;
    private JButton button2;
    private JPanel panel1;
    private JLabel label3;
    private JScrollPane scrollPane2;
    private JTextArea topicContentArea;
    private JLabel topicNameLabel;
    private JTextField topicNameTxt;
    private JLabel topicPublisherLabel;
    private JTextField topicPublisherTxt;
    // JFormDesigner - End of variables declaration  //GEN-END:variables

    /**
     * 给表格添加鼠标点击的事件监听
     * @param e
     */
    private void tableMousePressed(MouseEvent e){
        int row=topicTable.getSelectedRow();
        Topic topic=list.get(row);
        topicNameTxt.setText(topic.getTopicName());
        topicPublisherTxt.setText(topic.getTopicPublisher());
        topicContentArea.setText(topic.getTopicContent());

        topicName=topic.getTopicName();
        topicPublisher=topic.getTopicPublisher();
        topicContent=topic.getTopicContent();
    }



}

